#include "Unicode2Utf8.h"


std::string unicode2utf8(unsigned int uc)
{
	unsigned char u1, u2, u3, u4;
	std::string ret;

	if (uc < 0x80) {
		return std::string(1,uc);
	} else if (uc < 0x800) {
		u2 = 0xC0 | uc >> 6;
		u1 = 0x80 | uc & 0x3F;
		ret+=u2;
		ret+=u1;
		//cout << "³¤¶È£º" << 2 << "×Ö½Ú" << endl;
		return ret;
	} else if (uc < 0x10000) {
		u3 = 0xE0 | uc >> 12;
		u2 = 0x80 | uc >> 6 & 0x3F;
		u1 = 0x80 | uc & 0x3F;
		ret+=u3;
		ret+=u2;
		ret+=u1;
		//cout << "³¤¶È£º" << 3 << "×Ö½Ú" << endl;
		return ret;
	} else if (uc < 0x200000) {
		u4 = 0xF0 | uc >> 18;
		u3 = 0x80 | uc >> 12 & 0x3F;
		u2 = 0x80 | uc >> 6 & 0x3F;
		u1 = 0x80 | uc & 0x3F;
		ret+=u4;
		ret+=u3;
		ret+=u2;
		ret+=u1;
		//cout << "³¤¶È£º" << 4 << "×Ö½Ú" << endl;
		return ret;
	}
	return "";
}



